//
//  FZNavigationController.m
//  EnglishTalk
//
//  Created by CyonLeuPro on 15/5/25.
//  Copyright (c) 2015年 Feizhu Tech. All rights reserved.
//

#import "FZNavigationController.h"
#import "FZStyleSheet.h"
#import "SRCommonViewController.h"

@implementation FZNavigationController

+ (void)initialize
{
//    UINavigationBar *navBar = [UINavigationBar appearance];
    
//    [navBar setBarTintColor:[UIColor colorWithHexString:@"F7F7F7" withAlpha:0.97]];
    
//    [navBar setBarTintColor:[FZStyleSheet defaultStyleSheet].colorOfLighterText];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self.navigationBar setBarTintColor:[UIColor colorWithHexString:@"F7F7F7" withAlpha:0.97]];
}

+ (UIBarButtonItem *)createNavigationBarButton:(NSString *)title target:(id)target action:(SEL)action
{
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:target action:action];
    return back;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [super pushViewController:viewController animated:animated];
}


- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    UIViewController *controller = [super popViewControllerAnimated:animated];
    // TODO
    return controller;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    if ([self.visibleViewController respondsToSelector:@selector(preferredInterfaceOrientationForPresentation)]) {
        return [self.visibleViewController preferredInterfaceOrientationForPresentation];
    }
    return  UIInterfaceOrientationPortrait;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if ([self.visibleViewController isKindOfClass:[UIAlertController class]]) {
        return UIInterfaceOrientationMaskPortrait;
    }
    if ([self.visibleViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
        return [self.visibleViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskPortrait;//self.visibleViewController.supportedInterfaceOrientations;
}

- (BOOL)shouldAutorotate {
    if ([self.visibleViewController respondsToSelector:@selector(shouldAutorotate)]) {
        return [self.visibleViewController shouldAutorotate];
    }
    return NO;
}

@end

//对返回按钮做特殊处理,如果实现了单独的doBack方法，需要判断返回逻辑的部分，直接调用代理方法，否则默认返回上一页
@implementation FZNavigationController (ShouldPopOnBackButton)

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
    UIViewController* vc = [self topViewController];
    
    //对于contact supplier成功页面做特殊处理
    if(([self.viewControllers count] < [navigationBar.items count])) {
        return YES;
    }
    
    //所有基类的处理方法,会调用doBack方法，如果有doBack方法
    if([vc isKindOfClass:[SRCommonViewController class]] &&  [vc respondsToSelector:@selector(doBack)]) {
        SRCommonViewController * viewControl=(SRCommonViewController *)vc;
        [viewControl doBack];
        return NO;
    }
    
    return YES;
}

@end
